package net.ddyule.www.ddyule;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.tencent.smtt.sdk.ValueCallback;

import java.util.HashMap;
import java.util.Locale;

public class AndroidToJs {
    //TextToSpeech tts;
    private tts_xunfei mtts_xunfei;
    //HashMap<String, String> map = new HashMap<String, String>();
    //int serailNum = 1;
    AndroidToJs() {
//        tts = new TextToSpeech(MainActivity.sContent, this);
//        tts.setOnUtteranceProgressListener(new ttsUtteranceListener());
        mtts_xunfei = new tts_xunfei();
        mtts_xunfei.init();
    }

    @JavascriptInterface
    public void speak(String msg) {
       // Toast.makeText(MainActivity.sContent, "speak", Toast.LENGTH_SHORT).show();
//        map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.valueOf(serailNum));
//        serailNum++;
//        return tts.speak(msg, TextToSpeech.QUEUE_FLUSH, map);
        mtts_xunfei.speak(msg);
    }


    @JavascriptInterface
    public void stop() {
        mtts_xunfei.stop();
    }

    @JavascriptInterface
    public void setPitch(int pitch) {
        //tts.setPitch(pitch);
        mtts_xunfei.setPitch(pitch);
    }

    @JavascriptInterface
    public void setSpeed(int speed) {
//        tts.setSpeechRate(speed);
        mtts_xunfei.setSpeed(speed);
    }

    @JavascriptInterface
    public boolean isSpeaking(){
//        return tts.isSpeaking();
        return mtts_xunfei.isSpeaking();
    }
    @JavascriptInterface
    public void setVolume(int volume) {
        mtts_xunfei.setVolume(volume);
    }
    @JavascriptInterface
    public void pause() {
        mtts_xunfei.pause();
    }
    @JavascriptInterface
    public void resume(){
        mtts_xunfei.resume();
    }

    @JavascriptInterface
    public void setSpeaker(String speaker){
        mtts_xunfei.setSpeaker(speaker);
    }

    @JavascriptInterface
    public String getSpeaker(){
       return mtts_xunfei.voicer;
    }

    public void onInit(int status) {
        //初始化成功的话，设置语音，这里我将它设置为中文
//        if (status == TextToSpeech.SUCCESS) {
//            int result = tts.setLanguage(Locale.CHINA);
//            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
//                Toast.makeText(MainActivity.sContent, "手机不支持中文TTS", Toast.LENGTH_SHORT).show();
//            }
//        }
    }

    public void onPause()
    {
        sendtojs("onPaused");
        mtts_xunfei.pause();
    }

    public void onReusme()
    {
        sendtojs("onResume");
        mtts_xunfei.resume();
    }

    private void sendtojs(String funName)
    {
        final int version = Build.VERSION.SDK_INT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            MainActivity.webView.evaluateJavascript("javascript:"+funName+"()", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    //此处为 js 返回的结果
                    // Toast.makeText(MainActivity.sContent, value, Toast.LENGTH_SHORT).show();
                }
            });
        }
        else
        {
            Toast.makeText(MainActivity.sContent, "您的手机系统版本太低了, 最低版本要求,4.4.0", Toast.LENGTH_SHORT).show();
        }
    }

    @JavascriptInterface
    public void vibrate(long lastTime, long quiteTime, boolean repeat)  //震动时间 安静时间 是否循环
    {
        Vibrator vibrator = (Vibrator)MainActivity.sContent.getSystemService(MainActivity.sContent.VIBRATOR_SERVICE);
        long[] patter = {quiteTime, lastTime, quiteTime, lastTime};
        int rept = repeat?0:-1;
        vibrator.vibrate(patter, rept);
    }

    @JavascriptInterface
    public void vibrate(long lastTime) //震动一次
    {
        Vibrator vibrator = (Vibrator)MainActivity.sContent.getSystemService(MainActivity.sContent.VIBRATOR_SERVICE);
        vibrator.vibrate(1000);
    }

    @JavascriptInterface
    public void vibrateCancel()     //取消震动
    {
        Vibrator vibrator = (Vibrator)MainActivity.sContent.getSystemService(MainActivity.sContent.VIBRATOR_SERVICE);
        vibrator.cancel();
    }

    @JavascriptInterface
    public boolean isScreenOn()     //屏幕是否点亮
    {
        PowerManager pm = (PowerManager) MainActivity.sContent.getSystemService(Context.POWER_SERVICE);
        return pm.isScreenOn();
    }

    @JavascriptInterface
    public void makeScreenOn()      //点亮屏幕
    {
        MainActivity.makeScreenOn();
    }
}

